package com.jtyoui.jsns;

import com.jtyoui.bean.Words;
import com.jtyoui.isns.Filter;

import java.util.*;

/**
 * 过滤词对象：暂时是个难点
 *
 * @author Jtyoui
 * @version 18.10.12
 */
public class JFilter implements Filter {
    private Set<String> filter = new HashSet<>();//过滤词对象
    private Map<String, Words> sns_word = null;//封装每个词对象，key是词、value是词对象

    /**
     * 过滤词语
     *
     * @param filterSolidify 凝聚度
     * @param filterFree     自由度
     */
    @Override
    public void filter(float filterSolidify, float filterFree, float shift) {
        Set<String> set = sns_word.keySet();
        for (String key : set) {
            Words words = sns_word.get(key);
            for (int i = 0; i < key.length() - 3; i++) {
                String k = key.substring(i, i + 2);
                if (sns_word.containsKey(k) && words.getTotal() == sns_word.get(k).getTotal())
                    filter.add(k);
            }
            if (words.getSolidify() <= filterSolidify || words.getFree() <= filterFree || words.getShift() <= shift) {
                filter.add(key);
            } else {
                for (Character leftKey : words.getLeftCount().keySet()) {
                    int leftValue = words.getLeftCount().get(leftKey);
                    if ((leftValue * 1.0f / words.getLeftWord().size()) > 0.5) {
                        key = leftKey + key;
                        words.setKey(key);
                        break;
                    }
                }
                for (Character rightKey : words.getRightCount().keySet()) {
                    int rightValue = words.getRightCount().get(rightKey);
                    if ((rightValue * 1.0f / words.getRightWord().size()) > 0.5) {
                        key += rightKey;
                        words.setKey(key);
                        break;
                    }
                }
            }
        }
        for (String key : sns_word.keySet()) {
            if (!filter.contains(key))
                System.out.println(sns_word.get(key));
        }
    }

    /**
     * 增加词对象：该词对象必须要继承JSns接口
     *
     * @param sns_word 词对象
     */
    public void setSns_word(Map<String, Words> sns_word) {
        this.sns_word = sns_word;
    }

}
